package com.lms.logisticsmanagementsystem.controller;

import com.lms.logisticsmanagementsystem.model.Customer;
import com.lms.logisticsmanagementsystem.model.Staff;
import com.lms.logisticsmanagementsystem.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @GetMapping("/toIndex")
    public String toIndex() {
        return "customer/customerInfo";
    }

    @GetMapping("/toAddCustomer")
    public String toAddCustomer() {
        return "customer/addCustomer";
    }

    @PostMapping("/addCustomer")
    @ResponseBody
    public String addCustomer(@RequestBody Customer customer) {
        System.out.println(customer);
        //数据导入数据库
        //导入成功返回true
        boolean flag = customerService.addCustomerInfo(customer);
        if (flag) {
            return "success";
        } else {
            return "fail";
        }
    }

    @PostMapping("/getCustomerInfo")
    @ResponseBody
    public Map<String, Object> getCustomerInfo(HttpServletRequest req) {
        //获取表格分页条件
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        //获取后台数据
        List<Customer> customerList = customerService.getCustomerInfo();
        List<Customer> curPageInfo = new ArrayList<>();
        Integer curPage = Integer.valueOf(page);
        Integer limitRows = Integer.valueOf(limit);
        //当前行数据索引
        Integer curIndex = (curPage - 1) * limitRows;
        //目标行数据索引
        Integer targetIndex = curIndex + limitRows;
        //数据总数
        Integer totalInfoSize = customerList.size();
        //如果目标行索引大于数据总数，则返回数据总数进行判断
        if (targetIndex > totalInfoSize) {
            targetIndex = totalInfoSize;
        }
        for (int i = curIndex; i < targetIndex; i++) {
            curPageInfo.add(customerList.get(i));
        }
        //设置为layui表格接收格式
        Map<String, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("msg", "");
        map.put("count", customerList.size());
        map.put("data", curPageInfo);
        return map;
    }

    @GetMapping("/toEditCustomer")
    public String editCustomerInfo(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        //aim to get customer information
        Customer customer = customerService.getCustomerInfoById(Integer.valueOf(id));
        model.addAttribute("customer",customer);
        System.out.println(customer);
        return "customer/editCustomer";
    }

    @PostMapping("/editCustomerInfo")
    @ResponseBody
    public String submitEditedCustomerInfo(@RequestBody Customer customer){
        //aim to submit edited customer information to the database, if success return true else return false
        boolean flag = customerService.submitEditedCustomerInfo(customer);
        if (flag){
            return "success";
        }else {
            return "fail";
        }
    }

    @PostMapping("/delCustomerInfo")
    @ResponseBody
    public String delStaffInfoById(@RequestParam("id")String id){
        if (id==null||id==""){
            return "infoError";
        }
        boolean flag = customerService.delCustomerInfoById(Integer.valueOf(id));
        if (flag==true){
            return "success";
        }else {
            return "fail";
        }
    }
}
